﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System.Web;
using System.Web.SessionState;

namespace NetJsWire.Direct
{
    /// <summary>
    ///   represents the HttpHandler Class that handles the all requests done by Ext.Direct's providers
    /// </summary>
    public class DirectRouter : IHttpHandler, IRequiresSessionState
    {
        #region IHttpHandler Members

        public void ProcessRequest( HttpContext context )
        {


            // set default contenttype to json
            context.Response.ContentType = "application/json";

            var responseWrapStart = "";
            var responseWrapEnd = "";

            // check if the request contains a Fileupload. If so the router needs to return an HTML Document containing a Textarea
            if ( context.Request.Files.Count > 0 )
            {
                // The server response is parsed by the browser to create the document for the IFRAME. 
                // If the server is using JSON to send the return object, then the Content-Type header must 
                // be set to "text/html" in order to tell the browser to insert the text unchanged into the document body.
                context.Response.ContentType = "text/html";
                responseWrapStart = "<html><textarea>";
                responseWrapEnd = "</textarea></html>";
            }

            // execute the Action
            // var rpc = new ExtRPC();


            var provider = DirectProxy.GetDirectProviderCache( "Ext.app.REMOTING_API" );
            var directExecution = DirectProcessor.Execute( provider, context );

            if ( directExecution.containsErrors )
            {
                context.Response.StatusCode = 207;
            }

            // send eventually wraped content back to the browser

            context.Response.Write( responseWrapStart );

            context.Response.Write( directExecution.jsonResponse );

            context.Response.Write( responseWrapEnd );

            context.Response.End();


        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        #endregion
    }
}
